<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="主单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入主单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--  <el-form-item label="退款单号" prop="refundNo">
        <el-input
          v-model="queryParams.refundNo"
          placeholder="请输入退款单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入申请用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入申请手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="handleAdd"
          v-hasPermi="['content:refund:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['content:refund:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['content:refund:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['content:refund:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="refundList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="30px" align="center" />
      <el-table-column label="主单号" align="center" width="120px" prop="orderNo" />
      <el-table-column label="退款单号" align="center" width="120px" prop="refundNo" />
      <el-table-column label="总金额" align="center" prop="refundAmt" />
      <el-table-column label="实际退款" align="center" prop="refundRealAmt" />
      <el-table-column label="完成时间" align="center" prop="finishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        width="160"
        label="退款状态"
        align="center"
        prop="refundStatus"
        :formatter="refundStatusFormat"
      />
      <el-table-column label="退款理由" width="150px" align="center" prop="refundReason" />
      <el-table-column label="退款信息" width="150px" align="center" prop="refundInfo" />
      <el-table-column label="申请用户" width="50px" align="center" prop="userName" />
      <el-table-column label="拒接理由" width="150px" align="center" prop="rejectReason"></el-table-column>
      <el-table-column align="center" label="操作" style="width:120px;" fixed="right">
        <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleView(scope.row)"
              v-hasPermi="['content:refund:preview']"
            >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />



    <!-- 添加或修改订单申请对话框 -->
    <el-dialog :title="title" :visible.sync="showFlag" width="500px" append-to-body>
      <el-form ref="form" :model="formInfo" :rules="rules" label-width="80px">
                  <div>
       <div  class="item"  style="display:inline-block;width:70%">订单号：{{orderDetail.orderNo}}</div>
        <div class="item" style="display:inline-block">总额：<div style="color:red;display:inline-block">{{orderDetail.amt}}</div> 元</div>
        </div>
        <div  class="item">创建订单时间：{{orderDetail.createTime}}</div>
        <div  class="item">支付状态：{{orderDetail.payStatus}}</div>
        <div  class="item">下单用户：{{orderDetail.userName}}</div>
        <div  class="item">下单手机号：{{orderDetail.phone}}</div>
        <div  class="item">快递信息：{{orderDetail.expressInfo==null?"暂无快递信息":orderDetail.expressInfo}}</div>
      <el-table
        v-loading="loading"
        :data="orderDetail.goodsList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="商品名称" align="center" prop="goodsName" />
        <el-table-column label="商品类型" align="center" prop="goodsType" />
            <el-table-column label="单价" align="center" prop="price" />
      </el-table>
        <el-form-item label="拒绝退款理由" label-width="100">
      <el-input
  type="textarea"
  :autosize="{ minRows: 2, maxRows: 4}"
  placeholder="拒绝理由"
  v-model="form.rejectReason">
</el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="refund">退 款</el-button> 
        <el-button @click="reject">拒绝退款</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRefund,
  getRefund,
  delRefund,
  addRefund,
  updateRefund,
  exportRefund,
} from "@/api/content/refund";
import {orderDetailList,refund,reject} from "@/api/content/order";

export default {
  name: "Refund",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 订单申请表格数据
      refundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      flag: false,
      // 退款状态字典
      refundStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: undefined,
        refundNo: undefined,
        userName: undefined,
      },
      // 表单参数
      form: {},
      formInfo: {},
      // 表单校验
      rules: {
        orderNo: [
          { required: true, message: "主单号不能为空", trigger: "blur" },
        ],
        refundNo: [
          { required: true, message: "退款单号不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        finishTime: [
          { required: true, message: "完成时间不能为空", trigger: "blur" },
        ],
      },
      showFlag:false,
        orderDetail:""
    };
  },
  created() {
    this.getList();
    this.getDicts("plat_refund_status").then((response) => {
      this.refundStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询订单申请列表 */
    getList() {
      this.loading = true;
      listRefund(this.queryParams).then((response) => {
        this.refundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 退款状态字典翻译
    refundStatusFormat(row, column) {
      return this.selectDictLabel(this.refundStatusOptions, row.refundStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.flag = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        orderNo: undefined,
        refundNo: undefined,
        createTime: undefined,
        refundAmt: undefined,
        refundRealAmt: undefined,
        finishTime: undefined,
        refundStatus: undefined,
        refundReason: undefined,
        refundInfo: undefined,
        userId: undefined,
        userName: undefined,
        rejectReason: undefined,
        phone: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRefund(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "退款申请";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateRefund(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addRefund(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 查看 */
    handleView(row) {
      console.log(row)
      var formData = new FormData();
      formData ={
        orderNo:row.orderNo
      }
       this.showFlag = true
       orderDetailList(JSON.stringify(formData)).then(response=>{
        console.log(response)
        this.orderDetail =  response.data;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除订单申请编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delRefund(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有订单申请数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportRefund(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        })
        .catch(function () {});
    },
    refund:function(){
      console.log("发起退款")
        console.log(this.form)
      var formData = new FormData();
      formData ={
        orderNo:this.orderDetail.orderNo
      }
       refund(JSON.stringify(formData)).then(response=>{
         console.log(response)
         if (response.code === 200) {
                this.msgSuccess("退款成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
    },
      reject:function(){
        console.log("发起拒绝")
        if(this.form.rejectReason==undefined||this.form.rejectReason==null){
           this.msgError("请说明拒绝理由");
          return;
        }
           console.log( this.form)
        reject(JSON.stringify(formData)).then(response=>{
         console.log(response)
         if (response.code === 200) {
                this.msgSuccess("拒绝退款");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });

      }
  },
};
</script>
<style rel="stylesheet/scss" lang="scss">
.item{
   font-size: 20px;
    margin: 15px;
}
</style>